#include "ad_caiwu_del.h"
#include "ui_ad_caiwu_del.h"
#include "dlcw.h"
#include "ad_caiwu.h"
#include "value.h"
#include <QMessageBox>
#include <QStandardItem>
#include <QDateTime>

int j;

ad_caiwu_del::ad_caiwu_del(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ad_caiwu_del)
{
    ui->setupUi(this);
    setWindowTitle("查询账单");
}

ad_caiwu_del::~ad_caiwu_del()
{
    delete ui;
}

void ad_caiwu_del::on_pushButton_2_clicked()//取消按钮
{
    this->close();
    ad_CaiWu *adcw=new ad_CaiWu;
    adcw->show();
}

void ad_caiwu_del::on_pushButton_clicked()
{

}

void ad_caiwu_del::on_serchbtn_clicked()//查询账单
{
    QStandardItem *aItem;
    //非法不执行下面函数
    if(judge()==0) return;
    IAEP mp, head;
    head = ss();                  //调用ss()函数，将该函数的返回值赋给head
    //printf("请输入您要查找的信息（输入年月日，空格隔开）：");
    //scanf("%d%d%d", &year, &month, &day);       //输入年月日
    //printf("时     间          金   额                 收入来源或支出目的");
    j = 0;            //给i，j初始化
    mp = head;               //注意，再次使p重新指向第一个结点，不要忘了
    while (mp != NULL)
    {
        if (mp->year == ui->yearbox->value() && mp->month == ui->monthbox->value() && mp->day == ui->daybox->value())
        {
            j++;//j是账单计数器
        }
        mp = mp->next;
    }
    if (j == 0)                   //如果j=0，即没有进入前面的搜索循环，也就是没有找到相应的信息
    {
        //printf("没有找到相应的信息！(按0返回，按1重新搜索)\n");
        QMessageBox::warning(this,"err","未找到当天相关账单");
        return;
    }
    QStringList headlist;
    headlist<<"时间"<<"金额"<<"收入来源或支出目的";
    themodel=new QStandardItemModel(j+1,3,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    j=0;
    mp=head;
    while (mp != NULL)
    {
        if (mp->year == ui->yearbox->value() && mp->month == ui->monthbox->value() && mp->day == ui->daybox->value())
        {
            j++;//j是账单计数器;
            //时间
            aItem=new QStandardItem(QString::asprintf("%d-%d-%d",mp->year, mp->month, mp->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(j-1,0,aItem);
            //数额
            aItem=new QStandardItem(QString::asprintf("%c%.2lf",mp->ch, mp->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(j-1,1,aItem);
            //原因
            aItem=new QStandardItem(QString::asprintf("%s",mp->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(j-1,2,aItem);
            mp->num = j;
        }
        mp = mp->next;
    }
}

int ad_caiwu_del::judge()//判断输入信息是否合法 返回0非法 返回1合法
{
    //寻找用户
    QString ctmname=ui->username->text().trimmed();
    if(ctmname.size()==0){
        QMessageBox::warning(this,"err","用户名不能为空");
        return 0;
    }
    std::string zjbl=_name;
    _name=ctmname.toStdString();
    int testAcc=1;
    Accp lp, head;
    head = ff();
    lp = head;
    std::string adminname;
    adminname="admin123";
    while (lp != NULL)   //遍历链表，寻找当前输入的账户的账号
    {
        if (strcmp(lp->name, _name.c_str()) == 0||strcmp(adminname.c_str(),_name.c_str())==0)
        {
            testAcc = 0;
            break;
        }
        lp = lp->next;
    }
    if (testAcc != 0)        //如果没有重复账号
    {
        QMessageBox::warning(this,"err","账号不存在");
        _name=zjbl;
        return 0;
    }
    //判断日期是否合法
    QDateTime ccc=QDateTime::currentDateTime();
    int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if ( ui->yearbox->value() < 0 ||ui->yearbox->value()>ccc.toString("yy").toInt())
    {
        QMessageBox::warning(this,"err","日期不合法，请检查日期");
        return 0;
    }
    if ( ui->monthbox->value() < 1 || ui->monthbox->value()> ccc.toString("MM").toInt() )
    {
        QMessageBox::warning(this,"err","日期不合法，请检查日期");
        return 0;
    }
    if ( ui->monthbox->value() == 2 )
    {
            if (  ui->yearbox->value() % 4 == 0 )
            {
                    monthDays[1] = 29;
            }
    }
    if ( ui->daybox->value() < 1 || ui->daybox->value() > monthDays[ui->monthbox->value()-1] )
    {
        QMessageBox::warning(this,"err","日期不合法，请检查日期");
        return 0;
    }
    //数据过关
    return 1;
}
